niu
<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input v-model="queryParams.keywords" placeholder="关键字" clearable @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="hover" class="data-table">
      <div class="data-table__toolbar">
        <div class="data-table__toolbar--actions">
          <el-button type="primary" plain icon="plus" @click="handleToCreate()">创建合同</el-button>
        </div>
      </div>

      <el-table ref="dataTableRef" v-loading="loading" :data="dataList" highlight-current-row border
        class="data-table__content">
        <el-table-column label="合同名称" prop="name" />
        <el-table-column label="甲方" prop="partyA" />
        <el-table-column label="乙方" prop="partyB" />
        <el-table-column label="房屋" prop="houseName" />
        <el-table-column label="套餐" prop="saleMealName" />
        <el-table-column label="签订日期" prop="signTime" align="center" width="120">
          <template #default="scope">
            {{ `${(scope.row.signTime && scope.row.signTime.split(" ")[0]) || "-"}` }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="150" align="center" prop="status">
          <template #default="scope">
            {{ `${Contract_Status_Label[scope.row.status]}` }}
          </template>
        </el-table-column>
        <el-table-column label="总金额" width="150" align="right" prop="amount">
          <template #default="scope">
            {{ `￥${scope.row.amount}` }}
          </template>
        </el-table-column>
        <el-table-column label="开始日期" prop="startTime" align="center" width="120">
          <template #default="scope">
            {{ `${(scope.row.startTime && scope.row.startTime.split(" ")[0]) || "-"}` }}
          </template>
        </el-table-column>
        <el-table-column label="结束日期" prop="endTime" align="center" width="120">
          <template #default="scope">
            {{ `${(scope.row.endTime && scope.row.endTime.split(" ")[0]) || "-"}` }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button :disabled="scope.row.systemd" type="primary" size="small" link icon="edit"
              @click="handleDetail(scope.row.id)">
              详情
            </el-button>
            <el-button :disabled="scope.row.systemd" v-if="scope.row.status == ContractStatus.WAIT_SIGN" type="danger"
              size="small" link icon="delete" @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="fetchData" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import ContractAPI, {
  Contract_Status_Label,
  ContractPageQuery,
  ContractPageVO,
  ContractStatus,
} from "@/api/flow/contract.api";
import router from "@/router";

defineOptions({
  name: "Contract",
  inheritAttrs: false,
});

const queryFormRef = ref();

const loading = ref(false);
const total = ref(0);

const queryParams = reactive<ContractPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 列表数据
const dataList = ref<ContractPageVO[]>([]);

// 获取数据
function fetchData() {
  loading.value = true;
  ContractAPI.getPage(queryParams)
    .then((data) => {
      dataList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 查询（重置页码后获取数据）
function handleQuery() {
  queryParams.pageNum = 1;
  fetchData();
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  fetchData();
}

// 跳转新增
function handleToCreate() {
  router.push({ path: "/contract/contractCreate" });
}

// 跳转详情
function handleDetail(id: string) {
  router.push({ path: "/contract/contractDetail", query: { id } });
}

// 删除
function handleDelete(id: string) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    loading.value = true;
    ContractAPI.deleteById(id)
      .then(() => {
        ElMessage.success("删除成功");
        handleResetQuery();
      })
      .finally(() => (loading.value = false));
  });
}

onMounted(() => {
  handleQuery();
});
</script>
